import { getStore, setStore } from "../../utils/store";
import { diff } from "../../utils/util";

const tagObj = {
  label: "", //标题名称
  value: "" //标题的路径
};

const homeTagObj = {
  label: "首页", //标题名称
  value: "/home/index" //标题的路径
};

export default {
  state: {
    tagList: getStore("tagList") || [],
    tag: getStore("tagList") || tagObj,
    homeTagObj: homeTagObj
  },
  mutations: {
    ADD_TAG: (state, action) => {
      state.tag = action;
      setStore("tag", state.tag);
      if (state.tagList.some(ele => diff(ele, action))) return;
      state.tagList.push(action);
      setStore("tagList", state.tagList);
    },
    DEL_TAG: (state, action) => {
      state.tagList = state.tagList.filter(item => {
        return !diff(item, action);
      });
      setStore("tagList", state.tagList);
    },
    DEL_ALL_TAG: state => {
      state.tagList = [state.homeTagObj];
      setStore("tagList", state.tagList);
    },
    DEL_TAG_OTHER: state => {
      state.tagList = state.tagList.filter(
        item => item.value === state.tag.value
      );
      setStore("tagList", state.tagList);
    }
  },
  actions: {}
};
